<template>
  <div style="display: flex; height: 100%">
    <department-screen
      v-model="list.filter.departmentId"
      :treeData.sync="tree"
      :curNode.sync="curNode"
    />

    <div class="app-container" style="flex: 1">
      <el-form
        ref="listFilterRef"
        :model="list.filter"
        inline
        class="list-filter"
      >
        <el-form-item prop="name">
          <el-input v-model="list.filter.name" placeholder="姓名" clearable />
        </el-form-item>
        <el-form-item prop="account" :rules="simplifyAccount">
          <el-input
            v-model="list.filter.account"
            placeholder="账号"
            clearable
          />
        </el-form-item>
        <el-form-item prop="phone" :verify="{ required: false, type: 'phone' }">
          <el-input
            v-model="list.filter.phone"
            placeholder="手机号"
            clearable
          />
        </el-form-item>
        <el-form-item prop="status">
          <Status v-model="list.filter.status" />
        </el-form-item>
        <el-form-item>
          <el-button @click="reset">重置</el-button>
        </el-form-item>
      </el-form>

      <div class="table-operation">
        <div>
          <AuthButton
            @click="create"
            name="新增"
            v-show="tree[0] && curNode.id !== tree[0].id"
          />

          <ExportExcel
            url="/sot-admin-api/sys/user/export"
            :param="list.filter"
          />
        </div>
      </div>

      <el-table v-loading="list.loading" :data="list.data" v-bind="tableProp">
        <el-table-column label="姓名" prop="name" />
        <el-table-column label="账号" width="150" prop="account" />
        <el-table-column label="手机号" width="150" prop="phone" />
        <el-table-column label="部门" prop="departmentName" />
        <el-table-column label="角色" width="150" prop="roleName" />
        <el-table-column label="启用状态" width="80" align="center">
          <template slot-scope="{ row: { userId, status } }">
            <KiPopSwitch
              v-bind="popSwitchProps(status)"
              @change="updateStatus({ id: userId, status: status ^ 1 })"
            />
          </template>
        </el-table-column>
        <!-- <el-table-column label="微信昵称" prop="wechatNickName" /> -->
        <el-table-column label="操作" align="center" width="300">
          <template slot-scope="{ row }">
            <!-- <AuthButton
                @click="getWxTicketid(row)"
                circle
                name="微信绑定"
                type="success"
              >
                <i class="el-icon-yjwl-weixin" />
              </AuthButton> -->
            <AuthButton
              @click="bindPhone(row)"
              circle
              name="手机号绑定"
              type="success"
            >
              <i class="el-icon-mobile-phone" />
            </AuthButton>
            <AuthButton
              @click="changePwd(row)"
              circle
              name="修改密码"
              type="success"
            >
              <i class="el-icon-lock" />
            </AuthButton>
            <AuthButton @click="u(row, 'cache')" name="编辑" />
            <AuthButton @click="d({ id: row.userId })" name="删除" />
          </template>
        </el-table-column>
      </el-table>
    </div>

    <KiFormDialog
      :show.sync="form.show"
      :title="formTitle"
      v-model="form.data"
      :submit="submit"
      :readonly="form.status === 'r'"
      ref="formRef"
      :loading="form.loading"
    >
      <template #el-form>
        <el-form-item label="姓名" prop="name" :verify="{ maxLength: 45 }">
          <el-input
            v-model="form.data.name"
            placeholder="请输入姓名"
            clearable
          />
        </el-form-item>
        <!-- :rules="[required__, account__]" -->
        <el-form-item label="账号" prop="account" verify>
          <el-input
            v-model="form.data.account"
            placeholder="请输入账号"
            clearable
          />
        </el-form-item>
        <el-form-item
          label="部门"
          prop="departmentId"
          verify
          v-if="form.status === 'u'"
        >
          <el-cascader
            v-model="form.data.departmentId"
            :options="tree[0] && tree[0].children"
            :props="defaultProps"
          />
        </el-form-item>
        <el-form-item label="角色" prop="roleId" verify>
          <KiSelect
            v-model="form.data.roleId"
            :options="roleOptions"
            :props="{ value: 'roleId', label: 'name' }"
          />
        </el-form-item>
        <el-form-item
          v-if="form.status == 'c'"
          label="密码"
          prop="password"
          verify
          :rules="[password]"
        >
          <el-input
            v-model="form.data.password"
            type="password"
            autocomplete="new-password"
            placeholder="请输入密码"
            clearable
          />
        </el-form-item>
      </template>
    </KiFormDialog>

    <!-- <bind-wx ref="bindWx" /> -->
    <bind-phone ref="bindPhone" />
    <change-pwd ref="changePwd" />
  </div>
</template>

<script>
import pageMixin from '@/utils/pageMixin'
import useAdmateAdapter from '@/utils/useAdmateAdapter'
// import bindWx from './components/bind-wx.vue'
import bindPhone from '../components/bind-phone.vue'
import changePwd from '../components/change-pwd.vue'
import departmentScreen from './components/department-screen'
import { password } from '@/utils/validate-ruler.js'

export default {
  mixins: [pageMixin],
  components: { bindPhone, changePwd, departmentScreen },
  setup: () =>
    useAdmateAdapter(
      {
        urlPrefix: 'sot-admin-api/sys/user',
        axiosConfig: {
          getList: {
            url: 'list'
          },
          d: {
            method: 'DELETE'
          }
        },
        list: {
          dataAt: 'data'
        }
      },
      {
        getListInitially: false
      }
    ),
  data () {
    return {
      password,
      defaultProps: {
        expandTrigger: 'hover',
        value: 'id',
        checkStrictly: true,
        children: 'children',
        label: 'label'
      },
      tree: [],
      curNode: {
        children: [],
        id: null
      },
      roleOptions: [],
      simplifyAccount: {
        trigger: 'blur',
        validator: (rule, value, callback) => {
          if (value != '' && !/^[A-Za-z\d]+$/.test(value)) {
            callback(new Error('需为数字或字母'))
          } else {
            callback()
          }
        }
      }
    }
  },
  created () {
    this.init()
  },
  methods: {
    getWxTicketid (row) {
      this.$refs.bindWx.getWxTicketid(row)
    },
    bindPhone (row) {
      this.$refs.bindPhone.open(row)
    },
    changePwd (row) {
      this.$refs.changePwd.open(row)
    },
    init () {
      this.$POST('sot-admin-api/sys/role/list').then(res => {
        this.roleOptions = res.data || []
      })
    },
    create () {
      this.form.data.departmentId = this.curNode.id
      this.c()
    },
    submit () {
      if (this.form.data.departmentId instanceof Array) {
        this.form.data.departmentId = this.form.data.departmentId.pop()
      }
      return this.submitForm()
    }
  }
}
</script>
